<?php

namespace Cms;

class Column extends \TreeModel {
    
    use \State;
    
    static public $table = 'cms_column';

    protected $_fillable = array(
        'id' => 'int',
        'name' => 'str',
        'title' => 'str',
        'short' => 'str',
        'keywords' => 'str',
        'description' => 'str',
        'thumb' => 'str',
        'banner' => 'str',
        'url' => 'str',
        'type' => 'str',
        'sort' => 'int',
        'state' => 'int',
        'parent_id' => 'int',
        'depth' => 'int',
        'parents' => 'str',
        'parents_reverse' => 'str',
    );
    
    const STATE_ON = 1;
    const STATE_OFF = -1;
    
    protected $_states = [
        self::STATE_ON => '显示',
        self::STATE_OFF => '隐藏',
    ];

    public function getByName($name, $field = '') {
        $where = array('name' => $name);
        if ($field != '') {
            return Db()->getOne(static::$table)->fields($field)->where($where)->result();
        }
        return Db()->getRow(static::$table)->where($where)->result();
    }

    public function incPv($id) {
        $where = array('id' => $id);
        $arrSets = 'pv = pv + 1';
        $result = Db()->update(static::$table, $arrSets, $where);
        return $result;
    }

    public function getList() {
        $where = array('state' => 1);
        return Db()->getAll(static::$table)->where($where)->order('sort desc, id desc')->result();
    }

    public function getListInIds($ids) {
        $where = array(
            'state' => 1,
            'id' => array('in' => $ids),
        );
        return Db()->getAll(static::$table)->where($where)->order('sort desc, id desc')->result();
    }
    
    public function getTypeAssoc($key = null) {
        $arr = array(
            'lists' => '列表',
            'gallery' => '画廊',
            'cinema' => '影院',
            'cover' => '封面',
            'single' => '单页',
            'url' => '跳转',
        );
        
        if ($key !== null) {
            if (array_key_exists(strval($key), $arr)) {
                return $arr[strval($key)];
            } else {
                return '';
            }
        } else {
            return $arr;
        }

    }

    public function getTitleAssoc($id = 0) {
        if ($id) {
            return parent::getc($id, 'title');
        }

        $rows = parent::getAll();
        $arr = array();
        foreach ($rows as $row) {
            $arr[$row['id']] = $row['title'];
        }
        return $arr;
    }

    public function getCacheList() {
        $rows = cache('common', 'column');
        if (!$rows) {
            $rows = $this->getList();
            cache('common', 'column', $rows);
        }
        return $rows;
    }

    private function _cache() {
        $rows = $this->getList();
        cache('common', 'column', $rows);
    }

    public function add($data) {
        $result = parent::add($data);
        if (!$result) {
            return false;
        }

        $this->_cache();

        return $result;
    }

    public function update($id, $data) {
        $result = parent::update($id, $data);
        if (!$result) {
            return false;
        }

        $this->_cache();

        return $result;
    }

    public function remove($id) {
        $result = parent::remove($id);
        if (!$result) {
            return false;
        }

        $this->_cache();

        return $result;
    }

}
